package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

class TableDemo4 extends JFrame implements ActionListener
{
  JTable jt;

  TableDemo4 (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a table with a default table model that specifies 10
   // rows by 10 columns dimensions.

   jt = new JTable (new DefaultTableModel (10, 10));

   // Assign column identifiers (headers) to the columns.

   String [] columnIDs =
   {
     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
   };

   ((DefaultTableModel) 
   jt.getModel ()).setColumnIdentifiers (columnIDs);

   // Place the table in a JScrollPane object (to allow the table to
   // be vertically scrolled and display scrollbars, as necessary).

   JScrollPane jsp = new JScrollPane (jt);

   // Add the JScrollPane object to the frame window's content pane.
   // That allows the table to be displayed within a displayed 
   // scroll pane. It also allows the table's column headers to be
   // displayed.

   getContentPane ().add (jsp);

   // Create a panel for positioning buttons.

   JPanel jp = new JPanel ();

   // Create an "All Columns" button, register the current TableDemo4
   // object as a listener to that button's action events, and add
   // that button to the panel.

   JButton jb = new JButton ("All Columns");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Last Column" button, register the current TableDemo4
   // object as a listener to that button's action events, and add
   // that button to the panel.

   jb = new JButton ("Last Column");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Next Column" button, register the current TableDemo4
   // object as a listener to that button's action events, and add
   // that button to the panel.

   jb = new JButton ("Next Column");
   jb.addActionListener (this);
   jp.add (jb);

   // Create an "Off" button, register the current TableDemo4 object
   // as a listener to that button's action events, and add that
   // button to the panel.

   jb = new JButton ("Off");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Subsequent Columns" button, register the current
   // TableDemo4 object as a listener to that button's action
   // events, and add that button to the panel.

   jb = new JButton ("Subsequent Columns");
   jb.addActionListener (this);
   jp.add (jb);

   // Add the panel to the south portion of the frame window's
   // content pane.

   getContentPane ().add (jp, BorderLayout.SOUTH);

   // Establish the frame's initial size as 600x250 pixels.

   setSize (600, 250);

   // Display the frame window and all contained components.

   setVisible (true);
  }

  public void actionPerformed (ActionEvent e)
  {
   // Identify the button that initiated the event.

   JButton jb = (JButton) e.getSource ();

   // Obtain the button's label.

   String label = jb.getText ();

   // Select appropriate resize mode.

   if (label.equals ("All Columns"))
     jt.setAutoResizeMode (JTable.AUTO_RESIZE_ALL_COLUMNS);
   else
   if (label.equals ("Last Column"))
     jt.setAutoResizeMode (JTable.AUTO_RESIZE_LAST_COLUMN);
   else
   if (label.equals ("Next Column"))
     jt.setAutoResizeMode (JTable.AUTO_RESIZE_NEXT_COLUMN);
   else
   if (label.equals ("Off"))
     jt.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
   else
     jt.setAutoResizeMode (JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
  }

  public static void main (String [] args)
  {
   // Create a TableDemo4 object, which creates the GUI.

   new TableDemo4 ("Table Demo #4");
  }
}